package com.xqb.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqb.common.core.domain.entity.SysUser;
import com.xqb.common.utils.DateUtils;
import com.xqb.modules.domain.po.InsInstitution;
import com.xqb.modules.domain.po.LesCourseTeacher;
import com.xqb.modules.domain.vo.CourseInsTeacherVo;
import com.xqb.modules.domain.vo.CourseTeacherVo;
import com.xqb.modules.mapper.InsInstitutionMapper;
import com.xqb.modules.mapper.LesCourseTeacherMapper;
import com.xqb.modules.service.ILesCourseTeacherService;
import com.xqb.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 课程老师关系Service业务层处理
 *
 * @author xqb
 * @date 2024-10-14
 */
@Service
public class LesCourseTeacherServiceImpl implements ILesCourseTeacherService {
    @Autowired
    private LesCourseTeacherMapper lesCourseTeacherMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private InsInstitutionMapper insInstitutionMapper;

    /**
     * 查询课程老师关系
     *
     * @param id 课程老师关系主键
     * @return 课程老师关系
     */
    @Override
    public LesCourseTeacher selectLesCourseTeacherById(Long id) {
        return lesCourseTeacherMapper.selectLesCourseTeacherById(id);
    }

    /**
     * 查询课程老师关系列表
     * 
     * @param lesCourseTeacher 课程老师关系
     * @return 课程老师关系
     */
    @Override
    public List<LesCourseTeacher> selectLesCourseTeacherList(LesCourseTeacher lesCourseTeacher)
    {
        return lesCourseTeacherMapper.selectLesCourseTeacherList(lesCourseTeacher);
    }

    /**
     * 新增课程老师关系
     *
     * @param lesCourseTeacher 课程老师关系
     * @return 结果
     */
    @Override
    public int insertLesCourseTeacher(LesCourseTeacher lesCourseTeacher) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUserId, lesCourseTeacher.getTeacherId());
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        if (sysUser != null) {
            throw new RuntimeException("老师已存在");
        }
        lesCourseTeacher.setCreateTime(DateUtils.getNowDate());
        return lesCourseTeacherMapper.insertLesCourseTeacher(lesCourseTeacher);
    }

    /**
     * 修改课程老师关系
     * 
     * @param lesCourseTeacher 课程老师关系
     * @return 结果
     */
    @Override
    public int updateLesCourseTeacher(LesCourseTeacher lesCourseTeacher)
    {
        lesCourseTeacher.setUpdateTime(DateUtils.getNowDate());
        return lesCourseTeacherMapper.updateLesCourseTeacher(lesCourseTeacher);
    }

    /**
     * 批量删除课程老师关系
     * 
     * @param ids 需要删除的课程老师关系主键
     * @return 结果
     */
    @Override
    public int deleteLesCourseTeacherByIds(Long[] ids)
    {
        return lesCourseTeacherMapper.deleteLesCourseTeacherByIds(ids);
    }

    /**
     * 删除课程老师关系信息
     *
     * @param id 课程老师关系主键
     * @return 结果
     */
    @Override
    public int deleteLesCourseTeacherById(Long id) {
        return lesCourseTeacherMapper.deleteLesCourseTeacherById(id);
    }

    /**
     * 根据课程ID获取课程老师信息和机构信息
     *
     * @param courseId 课程ID
     * @return CourseInsTeacherVo对象，包含课程老师信息和机构信息
     */
    @Override
    public CourseInsTeacherVo getCourseTeacherAndIns(Long courseId) {
        // 创建一个CourseInsTeacherVo对象
        CourseInsTeacherVo courseInsTeacherVo = new CourseInsTeacherVo();
        // 通过课程ID查询课程老师列表
        List<CourseTeacherVo> courseTeacherVoList = lesCourseTeacherMapper.getCourseTeachers(courseId);

        // 判断课程老师列表不为空且列表中有元素
        if (courseTeacherVoList != null && !courseTeacherVoList.isEmpty()) {
            // 获取第一个老师的机构ID
            CourseTeacherVo courseTeacherVo = courseTeacherVoList.get(0);
            if (courseTeacherVo != null) {
                Long insId = courseTeacherVo.getInsId();
                // 新增对insId的非空判断
                if (insId != null) {
                    // 通过机构ID查询机构信息
                    InsInstitution insInstitution = insInstitutionMapper.selectInsInstitutionById(insId);
                    // 判断查询到的机构信息不为空
                    if (insInstitution != null) {
                        // 设置机构ID
                        courseInsTeacherVo.setInsId(insInstitution.getId());
                        // 设置机构Logo
                        courseInsTeacherVo.setLogo(insInstitution.getLogo());
                    }
                }
            }
            // 设置课程老师列表
            courseInsTeacherVo.setCourseTeacherVoList(courseTeacherVoList);
        }
        // 返回CourseInsTeacherVo对象
        return courseInsTeacherVo;
    }

}
